package com.xinzhi.controller;



import com.baomidou.mybatisplus.extension.api.R;
import com.xinzhi.anno.CheckRole;
import com.xinzhi.domain.User;
import com.xinzhi.domain.dto.AddProductDto;
import com.xinzhi.domain.dto.QueryProductDto;
import com.xinzhi.domain.dto.UpdateProductDto;
import com.xinzhi.service.LoginService;
import com.xinzhi.service.ProductService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    LoginService loginService;
    /**
     * 前端获取所有商品
     */
    @RequestMapping("/mtyx/getAllProduct")
    public Result getAllProduct(){
        return productService.getAllProduct();
    }

    /**
     * 获取商品详情
     * @param productId      商品 id
     * @return
     */
    @RequestMapping("/mtyx/getProductDetail")
    public Result getProductDetail(String productId){


        return productService.getProductDetail(productId);
    }

    /**
     * 根据商品二级类目获取商品
     * @param twoTypeId
     * @return
     */
    @RequestMapping("/mtyx/getProductByTwoType")
    public Result getProductByTwoType(String twoTypeId,String current){

        return productService.getProductByTwoType(twoTypeId,current);
    }

    /**
     * 根据商品一级类目 获取商品
     * @param oneTypeId
     * @return
     */
    @RequestMapping("/mtyx/getProductByOneType")
    public Result getProductByOneType(String oneTypeId){

        return productService.getProductByOneType(oneTypeId);
    }

    /**
     * 获取商品销量最好的前 10 个商品 作为页面推荐
     * @return
     */
    @RequestMapping("/mtyx/getSalesVolumeTop10")
    public Result getSalesVolumeTop(){

        return productService.getProductBySalesVolumeTop();
    }

    //=========================================================================================================

    /**
     * 新增商品
     * @param addProductDto
     * @return
     */
    @RequestMapping("/admin/addProduct")
    @CheckRole(role = {"admin"})
    public Result addProduct(@RequestBody AddProductDto addProductDto, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.addProduct(addProductDto);
    }

    /**
     * 商品管理页面
     * @param current       页码
     * @return
     */
    @RequestMapping("/admin/productManage")
    @CheckRole(role = {"admin"})
    public Result addProduct(String current, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.productManage(current);
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @RequestMapping("/admin/productDetail")
    @CheckRole(role = {"admin"})
    public Result productDetail(String id, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.productDetail(id);
    }

    /**
     * 修改商品
     * @param updateProductDto    请求参数
     * @return
     */
    @RequestMapping("/admin/updateProduct")
    @CheckRole(role = {"admin"})
    public Result updateProduct(@RequestBody UpdateProductDto updateProductDto, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.updateProduct(updateProductDto);
    }


    /**
     * 条件查询商品
     * @param queryProductDto    请求参数
     * @return
     */
    @RequestMapping("/admin/queryProduct")
    @CheckRole(role = {"admin"})
    public Result queryProduct(@RequestBody QueryProductDto queryProductDto, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.queryProduct(queryProductDto);
    }

    /**
     * 删除商品
     * @param id    商品id
     * @return
     */
    @RequestMapping("admin/delProduct")
    @CheckRole(role = {"admin"})
    public Result delProduct(String id, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.delProduct(id);
    }

    /**
     * 修改商品上架状态
     * @param id                 商品id
     * @param putAwayStatus      上架状态
     * @return
     */
    @RequestMapping("admin/putAwayOrOutOfProduct")
    @CheckRole(role = {"admin"})
    public Result putAwayOrOutOfProduct(String id,String putAwayStatus, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productService.putAwayOrOutOfProduct(id,putAwayStatus);
    }






}
